<?php

class MY_Controller Extends CI_Controller
{
	protected $layout_view = 'application_template';
    protected $content_view ='';
    protected $view_data = array();
	protected $page_title = 'Welcome';
	
	public function __construct()
	{
		parent::__construct();
		$this->_secure_page();
		$this->output->enable_profiler(FALSE);
	}
	
	public function _output($output)
    {
		$this->view_data['page_title'] = $this->page_title;
        if($this->content_view !== FALSE && empty($this->content_view)) $this->content_view = $this->router->class . '/' . $this->router->method;
        
        $yield = file_exists(APPPATH . 'views/' . $this->content_view . EXT) ? $this->load->view($this->content_view, $this->view_data, TRUE) : FALSE ;
        
        if($this->layout_view)
            echo $this->load->view('layouts/' . $this->layout_view, array('yield' => $yield), TRUE);
        else
            echo $yield;
			echo $output;
    }
	
	protected function _msg($type = FALSE,$message = FALSE,$redirect = FALSE)
	{
		$type = strtolower($type);
		switch($type)
		{
		 	case $type == 'e':
				$template = "<div class='alert alert-error'>".$message."</div>";
			break;
			case $type == 's';
				$template = "<div class='alert alert-success'>".$message."</div>";
			break;
			case $type == 'i';
				$template = "<div class='alert alert-info'>".$message."</div>";
			break;
			case $type == 'n';
				$template = "<div class='alert alert-notice'>".$message."</div>";
			break;
			case $type == FALSE;
				$template = htmlspecialchars($message);
			break;
		}
		
		if($type AND $message AND $redirect)
		{
			$this->session->set_flashdata('system_message',$template);
			redirect($redirect);
		}elseif($type AND $message AND $redirect == FALSE){
			return $template;
		}
		
		if($redirect == FALSE AND $message == FALSE AND $redirect == FALSE)
		{
			return $this->session->flashdata('system_message');
		}
	}
	
	protected function _secure_page()
	{
		$class = strtolower($this->router->class); 
		$method = strtolower($this->router->method);
		if($this->session->userdata('isloggedtrue') == FALSE)
		{
			if($class !== 'auth' AND $method !== 'login'AND $class !== 'users' )
			{
				redirect('auth/login');
			}
		}else{
			if($class == 'auth' AND $method !== 'logout' )
			{
				redirect('home');
			}
		}
	
	
	
	}
}